package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.constant.Global;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.controller.mng.MngUserActionStatisticsController;
import com.homedo.odin.solution.model.entity.UserActionLog;
import com.homedo.odin.solution.model.request.mng.useractionlog.UserActionLogRequestBean;
import com.homedo.odin.solution.service.UserActionLogServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author chenlei
 * @date 2018/5/24
 */
@Api(description = "用户行为统计", tags = {"UserActionLog"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/statistics")
public class UserActionLogController extends BaseController<UserActionLogServiceImpl> {

    @ApiOperation(value = "PC_记录用户行为日志", httpMethod = "POST")
    @PostMapping("/{bizType}/{bizId}")
    public ResultData getLogsStatistics(
            @ApiParam(value = "业务类型", required = true, allowableValues = MngUserActionStatisticsController.BIZ_TYPE) @PathVariable(value = "bizType") String bizType,
            @ApiParam(value = "业务标识", required = true) @PathVariable(value = "bizId") Long bizId,
            HttpServletRequest request,
            @Valid @RequestBody UserActionLogRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }
        UserActionLog log = new UserActionLog();
        BeanUtils.copyProperties(requestBean, log);
        log.setBusinessType(bizType);
        log.setBusinessId(bizId);
        log.setIp(request.getRemoteAddr());
        log.setReferer(request.getHeader(Global.REQ_REFERER));
        baseService.recordLog(log);

        return ResultData.newInstance();
    }
}
